package cn.ht.jywrwater.module.ht.controller.admin.villagemeter;

import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;
import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import cn.ht.jywrwater.module.ht.controller.admin.villagemeter.vo.*;
import cn.ht.jywrwater.module.ht.convert.villagemeter.VillageMeterConvert;
import cn.ht.jywrwater.module.ht.dal.dataobject.villagemeter.VillageMeterDO;
import cn.ht.jywrwater.module.ht.service.villagemeter.VillageMeterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 水表村庄绑定关系")
@RestController
@RequestMapping("/ht/village-meter")
@Validated
public class VillageMeterController {

    @Resource
    private VillageMeterService villageMeterService;

    @PostMapping("/create")
    @Operation(summary = "创建水表村庄绑定关系")
    @PreAuthorize("@ss.hasPermission('ht:village-meter:create')")
    public CommonResult<Long> createVillageMeter(@Valid @RequestBody VillageMeterCreateReqVO createReqVO) {
        return success(villageMeterService.createVillageMeter(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水表村庄绑定关系")
    @PreAuthorize("@ss.hasPermission('ht:village-meter:update')")
    public CommonResult<Boolean> updateVillageMeter(@Valid @RequestBody VillageMeterUpdateReqVO updateReqVO) {
        villageMeterService.updateVillageMeter(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水表村庄绑定关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:village-meter:delete')")
    public CommonResult<Boolean> deleteVillageMeter(@RequestParam("id") Long id) {
        villageMeterService.deleteVillageMeter(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水表村庄绑定关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:village-meter:query')")
    public CommonResult<VillageMeterRespVO> getVillageMeter(@RequestParam("id") Long id) {
        VillageMeterDO villageMeter = villageMeterService.getVillageMeter(id);
        return success(VillageMeterConvert.INSTANCE.convert(villageMeter));
    }

    @GetMapping("/list")
    @Operation(summary = "获得水表村庄绑定关系列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:village-meter:query')")
    public CommonResult<List<VillageMeterRespVO>> getVillageMeterList(@RequestParam("ids") Collection<Long> ids) {
        List<VillageMeterDO> list = villageMeterService.getVillageMeterList(ids);
        return success(VillageMeterConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水表村庄绑定关系分页")
    @PreAuthorize("@ss.hasPermission('ht:village-meter:query')")
    public CommonResult<PageResult<VillageMeterRespVO>> getVillageMeterPage(@Valid VillageMeterPageReqVO pageVO) {
        PageResult<VillageMeterDO> pageResult = villageMeterService.getVillageMeterPage(pageVO);
        return success(VillageMeterConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水表村庄绑定关系 Excel")
    @PreAuthorize("@ss.hasPermission('ht:village-meter:export')")
    @OperateLog(type = EXPORT)
    public void exportVillageMeterExcel(@Valid VillageMeterExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<VillageMeterDO> list = villageMeterService.getVillageMeterList(exportReqVO);
        // 导出 Excel
        List<VillageMeterExcelVO> datas = VillageMeterConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "水表村庄绑定关系.xls", "数据", VillageMeterExcelVO.class, datas);
    }

}
